Imports System.Drawing
Imports System.Data
Public Class Rpt_Charts
    Private moChart As jChart.GraphChart

#Region "Database Size Chart"
    Public Function MakeDatabaseSizeChart( _
                    ByVal sDBID As String, _
                    ByVal sPath As String) As String

        Dim oFactory As New SQLAutoDocLib.TableSize_Factory
        Dim oData As DataTable = oFactory.AverageWeeklyDatabaseSize(12, sDBID)

        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 460)
        moChart.ChartName = "Database Size - Last 12 weeks"

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = oData.Rows.Count
        Dim lMax As Long = 0

        Dim oXAxisValues(lCount - 1) As Single
        Dim oXAxisLabels(lCount - 1) As String
        Dim oSeriesValues(lCount - 1) As Single
        Dim oYAxisLabels(5) As String

        For i As Integer = 0 To lCount - 1
            oXAxisValues(i) = i
            oXAxisLabels(i) = oData.Rows(i).Item("dtWeekDate")

            If IsNumeric(oData.Rows(i).Item("AvgSize")) Then
                oSeriesValues(i) = oData.Rows(i).Item("AvgSize")

                If oData.Rows(i).Item("AvgSize") > lMax Then
                    lMax = oData.Rows(i).Item("AvgSize")
                End If
            Else
                oSeriesValues(i) = -999
            End If
        Next

        For i As Integer = 0 To 5
            oYAxisLabels(i) = FormatSize((lMax / 5) * i)
        Next

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)
        oYAxis1.YAxisLabels = oYAxisLabels

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a line series.
        Dim oIndicator As New jChart.Indicator( _
                    jChart.Indicator.Type.Cross, _
                    Color.Green, _
                    15)

        Dim oLineSeries As New jChart.Overlay.Series.LineSeries(oIndicator)
        With oLineSeries
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Database Size"

            '---Now we'll give the series the values it should use.
            .Values = oSeriesValues

            '---The series will be green in color
            .Color = Color.Green

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .LineType = Drawing2D.DashStyle.Solid
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oLineOverlay As jChart.Overlay.LineOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oLineOverlay = New jChart.Overlay.LineOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oLineOverlay.Series.Add(oLineSeries)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oLineOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    sPath, _
                    System.Drawing.Imaging.ImageFormat.Jpeg)

        Return moChart.FileName
    End Function

    Private Function FormatSize(ByVal oValue As Object) As String
        Dim sResult As String = ""

        Dim lSize As Long = 0

        If Not oValue Is System.DBNull.Value Then
            lSize = Val(oValue)
        End If

        Dim sSize As String = ""
        If lSize = 0 Then
            sSize = "0 kb"

        ElseIf lSize < 2500 Then
            sSize = FormatNumber(lSize, 0, 0, 0, TriState.True) & " kb"

        ElseIf lSize < 1050000 Then
            sSize = FormatNumber(lSize / 1024, 0, 0, 0, TriState.True) & " mb"

        Else
            sSize = FormatNumber((lSize / 1024) / 1024, 0, 0, 0, TriState.True) & " gb"

        End If

        Return sSize
    End Function
#End Region

#Region "Connection Chart"
    Public Function MakeDayDatabaseConnectionChart( _
                    ByVal sDBID As String, _
                    ByVal sDay As String, _
                    ByVal sPath As String) As String

        Dim oFactory As New SQLAutoDocLib.ConnectLog_Factory

        Dim oCurrent As DataTable = oFactory.AverageDayConnectionCounts(sDay, sDBID)
        Dim oPrevious As DataTable = oFactory.AverageDayConnectionCounts(CDate(sDay).AddDays(-1).ToShortDateString, sDBID)
        Dim oAverage As DataTable = oFactory.AverageDayConnectionCounts(CDate(sDay).AddDays(-1).ToShortDateString, sDBID, 28)

        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(510, 460)
        moChart.ChartName = "Average Connections - Day " & sDay

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim lCount As Integer = oCurrent.Rows.Count
        Dim lMax As Long = 0

        Dim oXAxisValues(lCount - 1) As Single
        Dim oXAxisLabels(lCount - 1) As String
        Dim oSeries1Values(lCount - 1) As Single
        Dim oSeries2Values(lCount - 1) As Single

        Dim oSeries3Values(lCount - 1) As Single
        Dim oSeries4Values(lCount - 1) As Single

        For i As Integer = 0 To lCount - 1
            oXAxisValues(i) = i
            oXAxisLabels(i) = CDate(oCurrent.Rows(i).Item("dtWeekDate")).TimeOfDay.ToString.Substring(0, 5)

            oSeries1Values(i) = oCurrent.Rows(i).Item("AvgCountOfAllConnections") - oCurrent.Rows(i).Item("AvgCountOfDBConnections")
            oSeries2Values(i) = oCurrent.Rows(i).Item("AvgCountOfDBConnections")
            oSeries3Values(i) = oPrevious.Rows(i).Item("AvgCountOfAllConnections")
            oSeries4Values(i) = oAverage.Rows(i).Item("AvgCountOfAllConnections")

            If oCurrent.Rows(i).Item("AvgCountOfAllConnections") > lMax Then
                lMax = oCurrent.Rows(i).Item("AvgCountOfAllConnections")
            End If

            If oPrevious.Rows(i).Item("AvgCountOfAllConnections") > lMax Then
                lMax = oPrevious.Rows(i).Item("AvgCountOfAllConnections")
            End If

            If oAverage.Rows(i).Item("AvgCountOfAllConnections") > lMax Then
                lMax = oAverage.Rows(i).Item("AvgCountOfAllConnections")
            End If
        Next

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels
        oXAxis.FontAngle = jChart.XAxis.Rotation.Diagonal

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, 5, 0, lMax)

        Dim oYLabels(5) As String
        For i As Integer = 0 To 5
            oYLabels(i) = (lMax / 5) * i
        Next

        oYAxis1.YAxisLabels = oYLabels

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Now that we have values, we'll need a series
        '   Our series will be a bar series.

        Dim oBarSeries1 As New jChart.Overlay.Series.StackedBarSeries
        With oBarSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Average Connections"

            '---Now we'll give the series the values it should use.
            .Values = oSeries1Values

            '---The series will be green in color
            .Color = Color.Green

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75
        End With

        Dim oBarSeries2 As New jChart.Overlay.Series.StackedBarSeries
        With oBarSeries2
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "DB Connections"

            '---Now we'll give the series the values it should use.
            .Values = oSeries2Values

            '---The series will be green in color
            .Color = Color.Aqua

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75
        End With

        Dim oLineSeries1 As New jChart.Overlay.Series.LineSeries(New jChart.Indicator(jChart.Indicator.Type.Cross, Color.PaleVioletRed, 5))
        With oLineSeries1
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            Dim sDOW As String = CDate(sDay).DayOfWeek.ToString

            .Name = "Average for last 4 " & sDOW & "s"

            '---Now we'll give the series the values it should use.
            .Values = oSeries4Values

            '---The series will be green in color
            .Color = Color.Salmon

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75
        End With

        Dim oLineSeries2 As New jChart.Overlay.Series.LineSeries(New jChart.Indicator(jChart.Indicator.Type.UpArrow, Color.Red, 5))
        With oLineSeries2
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Yesterday"

            '---Now we'll give the series the values it should use.
            .Values = oSeries3Values

            '---The series will be green in color
            .Color = Color.Chartreuse

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.5
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oBarOverlay As jChart.Overlay.StackedBarOverlay
        Dim oLineOverlay As jChart.Overlay.LineOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oBarOverlay = New jChart.Overlay.StackedBarOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)
        oLineOverlay = New jChart.Overlay.LineOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay
        oBarOverlay.Series.Add(oBarSeries1)
        oBarOverlay.Series.Add(oBarSeries2)

        oLineOverlay.Series.Add(oLineSeries1)
        oLineOverlay.Series.Add(oLineSeries2)

        '---Let's add the overlay to the chart.
        moChart.OverLays.Add(oBarOverlay)
        moChart.OverLays.Add(oLineOverlay)

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _
                System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    sPath, _
                    System.Drawing.Imaging.ImageFormat.Jpeg)

        Return moChart.FileName
    End Function

#End Region
End Class
